<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="分类名称" prop="auditTypeName">
        <el-input v-model="queryParams.auditTypeName" placeholder="请输入分类名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="完成状态" prop="completeStatus">
        <el-select v-model="queryParams.completeStatus" placeholder="请选择完成状态" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.COMPLETE_STATUS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table border row-key="id" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="区域" align="center">
        <template #default="scope">
          {{ scope.row.planAreaRespVO?.areaName }}
        </template>
      </el-table-column>
      <el-table-column label="分类名称" align="center" prop="auditTypeName" min-width="120" />
      <el-table-column label="审核项" align="center" prop="configName" min-width="200" />
      <el-table-column label="填报结果" align="center" prop="auditResult" min-width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.AUDIT_RESULT" :value="scope.row.auditResult" />
        </template>
      </el-table-column>
      <el-table-column label="问题简述" align="center" prop="remark" min-width="120" />
      <el-table-column label="责任人" align="center" prop="dutyUserId" />
      <el-table-column label="计划时间" align="center" prop="planDate" min-width="100" />
      <el-table-column label="附件" min-width="320px" align="center">
        <template #default="scope">
          <UploadFile :isShowTip="false" v-model="scope.row.files" :disabled="true" />
        </template>
      </el-table-column>

      <el-table-column label="不符合图片" min-width="120px" align="center">
        <template #default="scope">
          <image-preview v-if="scope.row.problemImg" :src="scope.row.problemImg" :width="50" :height="50" />
        </template>
      </el-table-column>

      <el-table-column label="扣分" align="center" prop="deductPoints" />
      <el-table-column label="整改部门" align="center" prop="repairDeptName" min-width="100" />
      <el-table-column label="措施" align="center" prop="takeWay" />
      <el-table-column label="整改图片" align="center" prop="repairImg" min-width="100" />
      <el-table-column label="完成状态" align="center" prop="completeStatus" min-width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMPLETE_STATUS" :value="scope.row.completeStatus" />
        </template>
      </el-table-column>
      <el-table-column label="问题备注" align="center" prop="problemRemark" min-width="100" />
      <el-table-column label="审核人" align="center" prop="auditUserName" />
      <el-table-column label="审核状态" align="center" prop="auditStatus" min-width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.AUDIT_STATUS" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>
      <el-table-column label="审核备注" align="center" prop="auditRemark" min-width="100" />
      <el-table-column label="审核时间" align="center" prop="auditTime" :formatter="dateFormatter" width="180px" />
      <el-table-column fixed="right" label="操作" align="center" min-width="180px">
        <template #default="scope">
          <el-button link type="primary" v-if="scope.row.completeStatus == 1" @click="openForm('update', scope.row.id)">
            编辑
          </el-button>
          <el-button link type="primary"
            v-if="scope.row.completeStatus != 1 && (scope.row.auditStatus != 2 && scope.row.auditStatus != 3)"
            @click="openForm('audit', scope.row.id)">
            审核
          </el-button>
          <el-button link type="primary" @click="openForm('view', scope.row.id)">
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <AuditPlanDetailForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { AuditPlanDetailApi, AuditPlanDetail } from '@/api/business/auditplandetail'
import AuditPlanDetailForm from './AuditPlanDetailForm.vue'

/** 审核填报 列表 */
defineOptions({ name: 'AuditPlanDetail' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<AuditPlanDetail[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  auditTypeName: undefined,
  auditResult: undefined,
  completeStatus: undefined,
  auditStatus: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await AuditPlanDetailApi.getAuditPlanDetailPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await AuditPlanDetailApi.deleteAuditPlanDetail(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 批量删除审核填报 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await AuditPlanDetailApi.deleteAuditPlanDetailList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch { }
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: AuditPlanDetail[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await AuditPlanDetailApi.exportAuditPlanDetail(queryParams)
    download.excel(data, '审核填报.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
